var EventHandler = {

	events: new Array(),

	defineEvent: function(eventName) {
		if (!this.events[eventName]) {
			this.events[eventName] = new Array();
		}
	},

	undefineEvent: function(eventName) {
		if (this.events[eventName]) {
			delete this.events[eventName];
		}
	},


	addListener: function(eventName, filter, callback) {
		if (!this.events[eventName]) {
			console.log(eventName + " is not a valid event");
			return;
		}

		var listenerDef = {
			"filter"	: filter,
			"callback"	: callback
		}

		this.events[eventName].push(listenerDef);
	},

	fireEvent: function(eventName, properties) {
		if (!this.events[eventName]) {
			console.log(eventName + " is not a valid event");
			return;
		}

		for (var i=0; i<this.events[eventName].length; i++) {
			if (this.events[eventName][i].filter == null) {
				//null denotes listning to all events
				this.events[eventName][i].callback(properties);
			} else {
				//Make sure all filter properties are defined before calling the callback
				var doCall = true;
				for (fi in this.events[eventName][i].filter) {
					if (!properties[fi]) {
						doCall = false;
						break;
					} else {
						if (!compare(properties[fi], this.events[eventName][i].filter[fi])) {
							doCall = false;
							break;
						}
					}
				}

				if (doCall) {
					this.events[eventName][i].callback(properties);
				}
			}
		}
	}

};